<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" language="javascript"
	src="<%=basePath%>JS/jquery-1.6.2.min.js"></script>


<script type="text/javascript" language="javascript"
	src="<%=basePath%>JS/encode/crypto.js"></script>
<script type="text/javascript" language="javascript"
	src="<%=basePath%>JS/encode/sha1.js"></script>
<script type="text/javascript" language="javascript"
	src="<%=basePath%>JS/encode/hmac.js"></script>
<script type="text/javascript" language="javascript"
	src="<%=basePath%>JS/encode/md5.js"></script>

<script type="text/javascript">


	//克隆对象函数
	function clone(myObj)
	{
	    if(typeof(myObj) != 'object') return myObj;
	    if(myObj == null) return myObj;
	    var myNewObj = new Object(); 
	    for(var i in myObj) myNewObj[i] = clone(myObj[i]); 
	    return myNewObj;
	}

	function randomString(length) {
	    var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'.split('');
	   
	    if (! length) {
	        length = Math.floor(Math.random() * chars.length);
	    }
	   
	    var str = '';
	    for (var i = 0; i < length; i++) {
	        str += chars[Math.floor(Math.random() * chars.length)];
	    }
	    return str;
	}

	$(function() {

		var params = {
				"oauth_consumer_key" : "3607363939",
				"oauth_nonce" :randomString(32),
				"oauth_signature_method" : "HMAC-SHA1",
				"oauth_timestamp" : Math.floor(new Date().getTime()/1000) + 300,
				"oauth_version" :  "1.0"
		};

	
		var httpMethod = encodeURIComponent("http://api.t.sina.com.cn/oauth/request_token");
		
		// 用来保存所有的属性名称和值
		var props = "";
		
		function allPrpos(obj) { 
		    // 开始遍历
		    for(var p in obj){ 
	            // p 为属性名称，obj[p]为对应属性的值
	            props = props + p + "%3D" + obj[p] +"%26";
		    } 
	
		    props = props.substring(0,props.length-3)
		    
		    // 最后显示所有的属性
		   // alert(props);
		}
		allPrpos(params);


		$("#props").html(props);

		
	var newParams = clone(params);



		
	var baseString = "GET&" + httpMethod + "&" + props;

	$("#baseString").html(baseString);
	var secret = "65d07369655bb4dfd05946d99340b9ec&";

	var bytes = Crypto.HMAC(Crypto.SHA1, baseString, secret, { asBytes: true }) ;
	//但仅仅这样还不行, 还得转换成Base64.
	//在Crypto.util下找到相应的方法.
	var signature = encodeURIComponent(Crypto.util.bytesToBase64(bytes));

	$("#signature").html(signature);

	//打印出时间 
	//$("#time").html( String(new Date().getTime()).substring(0,10));
	

	newParams["oauth_signature"] = signature;


	//拼装Authorization
	var authorization = "";
	function getAuthorization(obj) { 
	    // 开始遍历
	    for(var p in obj){ 
            // p 为属性名称，obj[p]为对应属性的值
            authorization = authorization + p + "=\"" + obj[p] +"\",";
	    } 
	    //截取最后一个“,”
	    authorization = "OAuth " + authorization.substring( 0 ,authorization.length -1);
	}
	getAuthorization(newParams);

	$("#authorization").html(authorization);



	

	$("#share").click(function() {
		$.ajax({
			type : "get",
			url : "http://api.t.sina.com.cn/oauth/request_token",
			beforeSend : function(req) {req.setRequestHeader("Authorization", authorization);},
		    //header: { 'Authorization': authorization},
			data : newParams,
			success : function(_result) {
				alert(_result);
			},
			error : function() {
				alert("无数据返回 ！");
			}
		});
	});


	//显示属性
	$("#xianshishuxing").click(function(){

		getObjpro(newParams);

	});

	

	

});

	//下面方法多余 ，只用于调用属性的显示
//==============================================================================================================
function getObjpro(obj) { 
    // 用来保存所有的属性名称和值
  	var pro = "";
    // 开始遍历
    for(var pr in obj){ 
        // 方法
        if(typeof(obj[pr])=="function"){ 
            obj[pr]();
        }else{ 
            // p 为属性名称，obj[p]为对应属性的值
            pro += pr + "=" + obj[pr] +"<br/>";
        } 
    } 

	$("#qingqiu").html(pro);
    
}
		

	
</script>
<title>共享</title>
</head>
<body>

<h2>用于计算签名的属性拼装值</h2>
<hr>
<div id="props"></div>

<h2>baseString</h2>
<hr>
<div id="baseString"></div>

<h2>signature</h2>
<hr>
<div id="signature"></div>


<h2>authorization</h2>
<hr>
<div id="authorization"></div>



<h2>请求</h2>
<hr>
<div id="qingqiu"></div>
<button id="share">响应</button>
<button id="xianshishuxing">显示属性</button>
</body>
</html>